package edison.jcr;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Iterator;

import javassist.URLClassPath;

import javax.jcr.ItemNotFoundException;
import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;

public class JcrPath {
	private final String path;
	private final int depth;
	private final String[] segments;

	JcrPath(String path) {
		if (path == null)
			throw new NullArgumentException("path");
		if (!path.startsWith("/") || path.endsWith("/"))
			throw new IllegalArgumentException(path);

		if (path.equals("/")) {
			this.path = "/";
			this.segments = new String[] { StringUtils.EMPTY };
			this.depth = 0;
		}
		else {
			this.path = path;
			this.segments = this.path.split("/");
			this.depth = segments.length - 1;
		}

		// QName qName = new QName("");
		// String[] s = new String[] { "/", ":", "[", "]", "|", "*" };
	}

	public String getName() {
		return segments[depth];
	}

	public String getParent() {
		return getAncestor(depth - 1);
	}

	public String getAncestor(int depth) {
		if (depth > this.depth)
			return null;
		if (depth == 0)
			return "/";
		StringBuilder pathBuilder = new StringBuilder();
		for (int i = 1; i < depth + 1; i++) {
			pathBuilder.append("/").append(segments[i]);
		}

		return pathBuilder.toString();
	}

	public String relPath(String relPath) {
		String absPath = path + "/" + relPath;
		return absPath.replaceAll("//", "/");
	}

	public int getDepth() {
		return depth;
	}

	@Override
	public String toString() {
		Segment[] segments = null;
		StringBuilder pb = new StringBuilder();
		for (Segment segment : segments) {
			if (segment.isRoot())
				pb.append("/");
			else if (segment.isIdentifier())
				pb.append("[").append(segment).append("]");
			else if (segment.isSelf())
				pb.append(".");
			else if (segment.isParent())
				pb.append("..");
			else if (segment.isName()) {
				pb.append(segment.getQualifiedName());
				if (segment.getIndex() > 1)
					pb.append("[").append(segment.getIndex()).append("]");
			}
			pb.append("/");
		}

		return StringUtils.removeEnd(pb.toString(), "/");
	}


	public static boolean isNodePath(String path) {
		return true;
	}

	public static void main(String[] args) throws ItemNotFoundException {

		JcrPath p = new JcrPath("/a/b");
		System.out.println(p.getAncestor(2));
		System.out.println(p.getName());
	}

	class Segment {

		private String qualifiedName;
		private int index;
		private String identifier;

		public boolean isRoot() {
			return true;
		}

		public boolean isIdentifier() {
			return true;
		}

		public boolean isSelf() {
			return true;
		}

		public boolean isParent() {
			return true;
		}

		public boolean isName() {
			return true;
		}

		public String getIdentifier(){
			return identifier;
		}
		
		public String getQualifiedName() {
			return qualifiedName;
		}

		public int getIndex() {
			return index;
		}

	}

}
